package com.beifu.manager.controller;

import com.beifu.manager.common.Const;
import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.model.AssetType;
import com.beifu.manager.model.AssetTypeExample;
import com.beifu.manager.model.Organizer;
import com.beifu.manager.model.OrganizerExample;
import com.beifu.manager.service.assettype.AssetTypeService;
import com.beifu.manager.service.organization.OrganizerService;
import com.mds.manager.utils.ListUtils;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping(value="/washingFactory", produces="text/html;charset=UTF-8")
public class WashingFactoryController {

    private Logger log = Logger.getLogger(this.getClass());


    @Autowired
    private OrganizerService organizerService;

    @RequestMapping("/typeIndex")
    public String goUserIndex(Model model){
        log.info("进入资产类别管理界面");
        return "/base/washtype/wash_type_index";
    }

    /**
     * 资产类别列表分页数据
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/getWashFactoryPage")
    public String getTypePage(Model model, HttpServletRequest request){
        log.info(">>> 进入洗涤厂类别分页列表");
        String page = request.getParameter("pageNumber");
        String contacts = request.getParameter("contacts");
        String washOrganizeName = request.getParameter("washOrganizeName");

        OrganizerExample example = new OrganizerExample();
        OrganizerExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(contacts)) {
            criteria.andContactsLike("%"+contacts+"%");
        }
        if (!StringUtils.isEmpty(washOrganizeName)) {
            criteria.andOrganizeNameLike("%"+ washOrganizeName +"%");
        }

        criteria.andOrganizeTypeEqualTo(Const.Organize_Type.WASHING_FACTORY);

        PageUtils Page = new PageUtils(page);
        Page.setQueryParames(example);
        Page = organizerService.getOrganizerPages(Page);
        return ResponseHelp.responseText(Page);
    }

    @RequestMapping("/goAddWashFactory")
    public String goAddTypePage(Model model){
        log.info("进入用户添加界面");
        return "/base/washtype/wash_type_add";
    }

    /**
     * 资产是否存在
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/isExist")
    public String isExist(Model model,HttpServletRequest request) {
        String assetsName = request.getParameter("organizeName");
        String id = request.getParameter("id");

        Integer idInt = Integer.valueOf(id);

        OrganizerExample example = new OrganizerExample();
        OrganizerExample.Criteria criteria = example.createCriteria();
        criteria.andOrganizeNameEqualTo(assetsName).andOrganizeTypeEqualTo(Const.Organize_Type.WASHING_FACTORY).andIdEqualTo(idInt);
        int byExample = organizerService.countByExample(example);
        if (byExample>0) {
            return ResponseHelp.responseErrorText("该洗涤厂已存在！");
        }
        return ResponseHelp.responseText();
    }



    /**
     * 添加资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addWashFactory")
    public String typeAdd(Model model,HttpServletRequest request){

        String organizeContacts = request.getParameter("organizeContacts");
        String organizeName = request.getParameter("organizeName");
        String contactsPhone = request.getParameter("contactsPhone");
        String organizeAddress = request.getParameter("organizeAddress");

        Organizer organizer = new Organizer();
        organizer.setContacts(organizeContacts);
        organizer.setOrganizeName(organizeName);
        organizer.setContactsPhone(contactsPhone);
        organizer.setOrganizeAddress(organizeAddress);
        organizer.setOrganizeType(Const.Organize_Type.WASHING_FACTORY);

        organizerService.insertSelective(organizer);


        return ResponseHelp.responseText();
    }

    @RequestMapping("/goUpdateWashFactoryPage")
    public String goUpdateTypePage(Model model,HttpServletRequest request){
        log.info("进入用户修改界面");

        String id = request.getParameter("id");

        Organizer organizer = organizerService.selectByPrimaryKey(Integer.decode(id));

        model.addAttribute("organizer", organizer);
        return "/base/washtype/wash_type_update";
    }

    /**
     * 更新资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/factoryUpdate")
    public String typeUpdate(Model model,HttpServletRequest request){
        String id = request.getParameter("id");

        String organizeContacts = request.getParameter("contacts");
        String organizeName = request.getParameter("organizeName");
        String contactsPhone = request.getParameter("contactsPhone");
        String organizeAddress = request.getParameter("organizeAddress");


        Organizer organizer = new Organizer();
        if (StringUtils.isBlank(id)) {
            return ResponseHelp.responseErrorText("请选择需要修改的条目！");
        }

        organizer.setId(Integer.decode(id));
        organizer.setContacts(organizeContacts);
        organizer.setOrganizeName(organizeName);
        organizer.setContactsPhone(contactsPhone);
        organizer.setOrganizeAddress(organizeAddress);
        organizer.setOrganizeType(Const.Organize_Type.WASHING_FACTORY);

        organizerService.updateByPrimaryKeySelective(organizer);

        return ResponseHelp.responseText();
    }

    /**
     * 删除资产
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/factoryDelete")
    public String typeDel(Model model,HttpServletRequest request){
        String ids = request.getParameter("ids");
        String[] idsArr = ids.split(",");
        organizerService.batchDel(idsArr);
        return ResponseHelp.responseText();
    }

}
